<template>
  <div class="app-container" style="padding: 0px;">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >添加视频</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pointVideoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序列号" align="center" prop="videoInfo.seqCode"/>
      <el-table-column label="名称" align="center" prop="videoInfo.name"/>
      <el-table-column label="ip地址" align="center" prop="videoInfo.ipCode"/>
      <el-table-column label="端口号" align="center" prop="videoInfo.portCode"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仪表信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="视频" prop="videoId" v-if="isAdd">
              <el-select v-model="form.videoId" style="width: 370px;" placeholder="——— 请选择要添加的视频 ———">
                <el-option
                  v-for="videoInfo in videoInfoList"
                  :key="videoInfo.id"
                  :label="`【${videoInfo.name}】=》${videoInfo.seqCode}`"
                  :value="videoInfo.id"
                ></el-option>
              </el-select>
            </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listPointVideo, getPointVideo, delPointVideo, addPointVideo, updatePointVideo, exportPointVideo } from "@/api/basic/pointVideo";
  import { listVideoInfo } from "@/api/basic/videoInfo";

  export default {
    props: ["pointId"],
    components: {
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 仪表信息表格数据
        pointVideoList: [],
        videoInfoList:[],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        isAdd: true,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          pointId: this.pointId,
        },
        // 表单参数
        form: {
          videoInfo:{}
        },
        // 表单校验
        rules: {
          videoId: [
            { required: true, message: "视频不能为空", trigger: "blur" }
          ],
        }
      };
    },
    watch: {
      pointId(){
        this.queryParams.pointId = this.pointId;
        this.getList();
      }
    },
    created() {
      this.getList();
      this.getVideoInfos();
    },
    methods: {
      // 表单重置
      reset() {
        this.form = {
          pointId: null,
          videoId: null,
        };
        this.resetForm("form");
      },
      /** 查询视频信息列表 */
      getVideoInfos() {
        let obj = {
          pageNum: 1,
          pageSize: 10000,
        }
        listVideoInfo(obj).then(response => {
          this.videoInfoList = response.rows;
        });
      },
      /** 查询视频信息列表 */
      getList() {
        this.loading = true;
        listPointVideo(this.queryParams).then(response => {
          this.pointVideoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.isAdd = true;
        this.open = true;
        this.title = "添加监测站点视频";
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.pointId = this.pointId;
            addPointVideo(this.form).then(response => {
              this.msgSuccess("添加成功");
              this.open = false;
              this.getList();
            });
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除视频信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPointVideo(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },

    }
  };
</script>
